/**
 * Windows form some propertis has set default value
 *
 * @author liuhz
 */
CQCIS.form.WinForm = Ext.extend(Ext.Window, {
    initState: Ext.emptyFn,
    saveState: Ext.emptyFn,
    action: 'add',
    width: 350,
    minWidth: 350,
    height: 250,
    closeAction: 'hide',
    layout: 'fit',
    plain: true,
    bodyStyle: 'padding:5px;',
    autoClose: true,
    modal: true,
    
    initComponent: function(){
        if (!this.buttons) {
            this.buttons = [{
                text: '确认',
                scope: this,
                handler: function(){
                    CQCIS.confirm('你确认要保存吗？', function(arg){
                        if (arg == 'yes') {
                            this.save();
                        }
                    }, this);
                }
            }, {
                text: '取消',
                scope: this,
                handler: function(){
                    this.hide();
                }
            }];
        }
        if (!this.keys) {
            this.keys = {
                key: 13,
                fn: function(){
                    CQCIS.confirm('你确认要保存吗？', function(arg){
                        if (arg == 'yes') {
                            this.save();
                        }
                    }, this);
                },
                scope: this
            };
        }
        
        CQCIS.form.WinForm.superclass.initComponent.call(this);
        this.addEvents({
            /**
             * @event on load
             */
            "submit": true
        });
    },
    
    // private
    onRender: function(ct, position){
        CQCIS.form.WinForm.superclass.onRender.call(this, ct, position);
        if (this.items && this.items.items) {
            for (var i = 0; i < this.items.items.length; i++) {
                var o = this.items.items[i];
                if (o instanceof CQCIS.form.DWRForm) {
                    this.form = o;
                    this.form.reset();
                    
                    if (this.autoClose === true || this.autoClose == 'true') {
                        this.form.on('saveafter', function(){
                            this.hide();
                        }, this);
                    }
                }
            }
        }
    },
    
    save: function(){
        if (!this.form) {
            this.fireEvent('submit', this.items.items[0], this);
        } else {
            if (this.action == 'add' || this.action == 'insert') {
                this.form.save();
            } else {
                this.form.update();
            }
        }
    },
    
    show: function(params){
        this.action = params ? params : 'add';
        CQCIS.form.WinForm.superclass.show.call(this);
        if (this.form && this.action == 'add') {
            this.form.reset();
        }
    },
    
    load: function(params){
        this.show(params ? 'edit' : 'add');
        if (this.form) {
            this.form.load(params);
        } else {
            for (var i = 0; i < this.items.items.length; i++) {
                var obj = this.items.items[i];
                if (obj instanceof CQCIS.grid.ListGrid) {
                    obj.store.proxy.params = params;
                    obj.store.reload();
                }
            }
        }
    }
});
Ext.reg('wform', CQCIS.form.WinForm);
